<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');	
	require_once('server_validations.inc');	
	
	session_start();
	check_permissions(CT_PERM_FIRME);
    select_language();
    
	if (isset($_POST['post_firma'])) {
		select_language();
		$db = db_connect();
		
		$nume = $_POST['nume'];
		$cf = $_POST['cf'];	
		$rc = $_POST['rc'];	
		$tel_fix = $_POST['tel_fix'];	
		$tel_fax = $_POST['tel_fax'];	
		$tel_mobil = $_POST['tel_mobil'];	
		$email = $_POST['email'];	
		$web = $_POST['web'];	
		$tip = $_POST['tip_firma'];	
		$adresa = $_POST['adresa'];	
		$oras = $_POST['oras'];	
		$judet = $_POST['judet'];	
		$cod_postal = $_POST['cod_postal'];	
		$tara = $_POST['tara'];	
		$comentariu = $_POST['comentariu'];
		
		$calitate_firma = $_POST['calitate_firma'];
		
		//server side validations
		$_SESSION['wrong_ids'] = array();

        /* nu mai este obligatoriu ca firma sa aibe cel putin un contact */
        /*
        $contacts = 0;
        for ($i = 0; $i < count($_SESSION['firma']->contacte); $i++)
            if ($_SESSION['firma']->contacte[$i]->status != CT_CONTACT_DELETED) {
                $contacts++;
                break;    
            }        
        
        if ($contacts < 1) {
            $_SESSION['wrong_ids']['contacte'] = 1;
            $mesaj_aditional = LANG_Trebuie_sa_adaugati_cel_putin_un_contact . '<br />';
        }
        */
		
		if (!validPrintable($nume, 1, CT_MAX_NUME_FIRMA_LENGTH)) {
			$_SESSION['wrong_ids']['nume'] = 1;
		}

		$_SESSION['firma']->nume = $nume;
			
			
		if (!validPrintable($cf, 0, CT_MAX_CF_LENGTH)) {
			$_SESSION['wrong_ids']['cf'] = 1;
		}

		$_SESSION['firma']->cf = $cf;
		
			
		if (!validPrintable($rc, 0, CT_MAX_RC_LENGTH)) {
			$_SESSION['wrong_ids']['rc'] = 1;
		}

		$_SESSION['firma']->rc = $rc;
		
			
		if (!validPhone($tel_fix, true) && $tel_fix != '') {
			$_SESSION['wrong_ids']['tel_fix'] = 1;
		}

		$_SESSION['firma']->tel_fix = $tel_fix;
			
			
		if (!validPhone($tel_fax, true) && $tel_fax != '') {
			$_SESSION['wrong_ids']['tel_fax'] = 1;
		}

		$_SESSION['firma']->tel_fax = $tel_fax;
		
			
		if (!validPhone($tel_mobil, true) && $tel_mobil != '') {
			$_SESSION['wrong_ids']['tel_mobil'] = 1;
		}

		$_SESSION['firma']->tel_mobil = $tel_mobil;
			
		
        /* nu mai este nevoie de introducerea obligatorie a unui numar de telefon */
        /*
        if ($tel_fix == '' && $tel_mobil == '') {
            $_SESSION['wrong_ids']['tel_fix'] = 1;
            $_SESSION['wrong_ids']['tel_mobil'] = 1;    
        }
        */
			
		if (!validMail($email) && $email != '') {
			$_SESSION['wrong_ids']['email'] = 1;
		}

		$_SESSION['firma']->email = $email;
			
			
		if (!validSite($web, false) && $web != '') {
			$_SESSION['wrong_ids']['web'] = 1;
		}

		$_SESSION['firma']->web = $web;
		
			
		$aux = filter_var($tip, FILTER_VALIDATE_INT);
		if ($aux === false) {
			$_SESSION['wrong_ids']['tip'] = 1;
		}

		$_SESSION['firma']->tip = $tip;				
			
			
		if (!validPrintable($adresa, 0, CT_MAX_ADRESA_LENGTH)) {
			$_SESSION['wrong_ids']['adresa'] = 1;
		}

		$_SESSION['firma']->adresa->adresa = $adresa;
			
			
		if (!validPrintable($oras, 0, CT_MAX_ORAS_LENGTH) && $oras != '') {
			$_SESSION['wrong_ids']['oras'] = 1;
		}

		$_SESSION['firma']->adresa->oras = $oras;
			
			
		if (!validPrintable($judet, 0, CT_MAX_JUDET_LENGTH)) {
			$_SESSION['wrong_ids']['judet'] = 1;
		}

		$_SESSION['firma']->adresa->judet = $judet;
		
			
		if (!validPrintable($cod_postal, 0, CT_MAX_COD_POSTAL_LENGTH)) {
			$_SESSION['wrong_ids']['cod_postal'] = 1;
		}

		$_SESSION['firma']->adresa->cod_postal = $cod_postal;
					
			
		if (!validPrintable($tara, 1, CT_MAX_TARA_LENGTH) && $tara != '') {
			$_SESSION['wrong_ids']['tara'] = 1;
		}

		$_SESSION['firma']->adresa->tara = $tara;
		
		$_SESSION['firma']->comentariu = $comentariu;
		$_SESSION['firma']->calitate_firma = $calitate_firma;
				
		if (count($_SESSION['wrong_ids']) > 0) {
            notice_msg($mesaj_aditional . LANG_Campurile_rosii_trebuiesc_completate_corect, 'add_firma.php');
		}

		if ($_SESSION['firma']->id_firma == CT_NOT_SAVED_YET) { 
			//new firma
			try {				
				$_SESSION['firma']->id_client = $_SESSION['user_logat']->id_client;

				$_SESSION['firma']->queryInsert($db);	

				unset($_SESSION['firma']);
				notice_msg(LANG_Firma_a_fost_adaugata, 'show_firms.php');	
			}
			catch (QueryInsertFirmaException $e) {
				redirectOnError(CT_ADD_FIRMA_INSERT_FIRMA);
			}
		}
		else {
			//edit firma
			try {				
				$_SESSION['firma']->queryUpdate($db);		
				unset($_SESSION['firma']);
				notice_msg(LANG_Datele_firmei_au_fost_modificate, 'show_firms.php');
			}
			catch (QueryUpdateFirmaException $e) {
				redirectOnError(CT_ADD_FIRMA_UPDATE_FIRMA);
			}
			catch (QueryWrongVersionException $e) {
				
			}
		}				
		//header("Location: show_firms.php");				
	}
	else {
		if (isset($_SESSION['wrong_ids']) && count($_SESSION['wrong_ids']) > 0) {
			$wrong_ids = $_SESSION['wrong_ids'];				
			unset($_SESSION['wrong_ids']);
		}
		else {
			//luat din URL id=ul firmaie
			if (isset($_GET['id']))	$id = $_GET['id'];
				
			$id_client = $_SESSION['user_logat']->id_client;
			
			if (isset($id) && !empty($id)) {
				//edit
				$db = db_connect();
				try {
					$_SESSION['firma'] = Firma::queryLoadById($db, $id, $id_client);
				}
				catch (QueryLoadFirmaException $e) {
//					redirectOnError(CT_LOAD_FIRMA_BY_ID);
				}
			}
			else {
				//add new
				$db = db_connect();
				$_SESSION['firma'] = new Firma($id_client, '', '', '', '', '', '', '', '', CT_TIP_FIRMA_SPEDITII);
			}
						
		}

		if (!isset($_GET['id'])) 
            html_header("LANG_ADD_FIRMA", null, "add_firma");
		else 
            html_header("LANG_Edit_Firma", null, "add_firma");
        ?>
		
		<script type="text/javascript">
            /* nu mai e necesar ca macar un telefon sa fie prezent */
            /*
            function checkTelefoane() {
                if (document.add_firma.tel_fix.value == '' && document.add_firma.tel_mobil.value == '') {
                    alert(LANG_minim_un_telefon);
                    return false;
                }
                else
                    return true; 
            }
            */
            
            /* nu mai e necesar ca cel putin un contact sa fie introdus */
            /*
            function checkContacts() {
                if (document.getElementById('contactsCount').innerHTML == '0') {
                    alert(LANG_minim_un_contact);
                    return false;
                }
                else
                    return true; 
            }
            */
		
			function validateForm() {				
				return (validatePrintable(document.add_firma.nume.value, '<?php echo LANG_NUME_FIRMA; ?>', true, 2) &&
							validatePrintable(document.add_firma.cf.value, '<?php echo LANG_CF; ?>', false, 5) &&
							validatePrintable(document.add_firma.rc.value, '<?php echo LANG_RC; ?>', false, 5) &&
							validatePosInteger(document.add_firma.tel_fix.value, '<?php echo LANG_TEL_FIX; ?>', false) &&					
							validatePosInteger(document.add_firma.tel_fax.value, '<?php echo LANG_TEL_FAX; ?>', false) &&					
							validatePosInteger(document.add_firma.tel_mobil.value, '<?php echo LANG_TEL_MOBIL; ?>', false) &&
							validateMail(document.add_firma.email.value, false) &&
							validateSite(document.add_firma.web.value, false, '<?php echo LANG_WEB; ?>') &&
							validatePrintable(document.add_firma.adresa.value, '<?php echo LANG_ADRESA; ?>', false, 5) &&
							validatePrintable(document.add_firma.oras.value, '<?php echo LANG_ORAS; ?>', false, 3) &&
							validatePrintable(document.add_firma.judet.value, '<?php echo LANG_JUDET; ?>', false, 2) &&
							validatePrintable(document.add_firma.cod_postal.value, '<?php echo LANG_COD_POSTAL; ?>', false, 1) &&
							validatePrintable(document.add_firma.tara.value, '<?php echo LANG_TARA; ?>', false, 2) &&
                            /* checkTelefoane() && */
                            checkContacts());
			}
		</script>
		
		<?php
		
		// ******** HEADER AND INCLUDES
		
		if (isset($_GET['id'])) 
            $text = LANG_Edit_Firma;
		else 
            $text = LANG_Add_Firm;
            
		$linkuri = array(LANG_Home => "home.php", LANG_Firme => "show_firms.php", $text => "");
		titlu_pagina($linkuri, "add_firma");
		
	    output_error_msg();
	    output_notice_msg();
        output_extra_text(LANG_EXTRA_ADD_FIRMA);
		?>
		
		<div class="container" align="left">
			<form name="add_firma" id="add_firma" action="add_firma.php" method="post" onsubmit="return (validateForm());">
		 		<table id="wrapper2" width="800">
		 			<thead>
		 				<tr>
		 					<th align="center" colspan="6"><?php echo LANG_Date_firma; ?></th>
		 				</tr>
		 			</thead>
			 		<tr>
				 		<td <?php check_field('nume'); ?>align="right"><label for="nume"><?php echo LANG_Nume_firma . CT_OBLIGATORIU; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_Nume_firma_tooltip); ?> size="25" type="text" id="nume" name="nume" maxlength="<?php echo CT_MAX_NUME_FIRMA_LENGTH; ?>" value="<?php echo $_SESSION['firma']->nume; ?>" />					
						</td>
				 		<td <?php check_field('cf'); ?>align="right"><label for="cf"><?php echo LANG_CF; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_cf_tooltip); ?> size="25" type="text" id="cf" name="cf" maxlength="<?php echo CT_MAX_CF_LENGTH; ?>" value="<?php echo $_SESSION['firma']->cf; ?>"/>					
						</td>
				 		<td <?php check_field('rc'); ?>align="right"><label for="rc"><?php echo LANG_RC; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_rc_tooltip); ?> size="25" type="text" id="rc" name="rc" maxlength="<?php echo CT_MAX_RC_LENGTH; ?>" value="<?php echo $_SESSION['firma']->rc; ?>"/>					
						</td>
			 		</tr>
			 		<tr>
				 		<td <?php check_field('tel_fix'); ?>align="right"><label for="tel_fix"><?php echo LANG_TEL_FIX /*. CT_OBLIGATORIU; */ ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_tel_fix_tooltip); ?> type="text" id="tel_fix" size="25" name="tel_fix" maxlength="<?php CT_MAX_PHONE_LENGTH; ?>" value="<?php echo $_SESSION['firma']->tel_fix; ?>"/>					
						</td>
				 		<td <?php check_field('tel_fax'); ?>align="right"><label for="tel_fax"><?php echo LANG_TEL_FAX; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_tel_fax_tooltip); ?> size="25" type="text" id="tel_fax" name="tel_fax" maxlength="<?php echo CT_MAX_PHONE_LENGTH; ?>" value="<?php echo $_SESSION['firma']->tel_fax; ?>"/>					
						</td>
				 		<td <?php check_field('tel_mobil'); ?>align="right"><label for="tel_mobil"><?php echo LANG_TEL_MOBIL /*. CT_OBLIGATORIU; */ ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_tel_mobil_tooltip); ?> size="25" type="text" id="tel_mobil" name="tel_mobil" maxlength="<?php echo CT_MAX_PHONE_LENGTH; ?>" value="<?php echo $_SESSION['firma']->tel_mobil; ?>"/>					
						</td>
			 		</tr>
			 		<tr>
				 		<td <?php check_field('email'); ?>align="right"><label for="email"><?php echo LANG_EMAIL; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_email_tooltip); ?> size="25" type="text" id="email" name="email" maxlength="<?php echo CT_MAX_MAIL_LENGTH; ?>" value="<?php echo $_SESSION['firma']->email; ?>"/>					
						</td>
				 		<td <?php check_field('web'); ?>align="right"><label for="web"><?php echo LANG_WEB; ?></label></td>
				 		<td>
							<input <?php echo show_tooltip(LANG_web_tooltip); ?> size="25" type="text" id="web" name="web" maxlength="<?php echo CT_MAX_SITE_LENGTH; ?>" value="<?php echo $_SESSION['firma']->web; ?>"/>					
						</td>
				 		<td <?php check_field('tip_firma'); ?>align="right"><label for="tip_firma"><?php echo LANG_TIP_FIRMA; ?></label></td>
				 		<td>
							<?php render_tip_firma("tip_firma", $_SESSION['firma']->tip); ?>
				 		</td>
			 		</tr>
			 		<tr>
			 			<td align="right">
			 				<?php echo LANG_Comentariu; ?>
			 			</td>
			 			<td align="left">
			 				<textarea name="comentariu" rows="2" cols="30"><?php echo $_SESSION['firma']->comentariu ?></textarea>
			 			</td>
			 			<td align="right">
			 				<?php echo LANG_Calitate_firma; ?>
			 			</td>
			 			<td colspan="3">
			 				<?php
			 					$data_calitate_firma = array(CT_FIRMA_SERIOASA => constant("LANG_Firma_serioasa"), CT_FIRMA_NESERIOASA => constant("LANG_Firma_neserioasa"));
								render_combobox("calitate_firma", array_keys($data_calitate_firma), array_values($data_calitate_firma), $_SESSION['firma']->calitate_firma);
			 				?>
			 			</td>
			 		</tr>
		 		</table>
		 </div>
	    			 	
				<div class="container" align="left">
		      		<table border="0" cellpadding="0" cellspacing="0" id="wrapper2" width="800">
		      			<thead>
		      				<tr>
		      					<th colspan="6" align="center"><?php echo LANG_Adresa_Firma; ?></th>
		      				</tr>
		      			</thead>
				 		<tr>
					 		<td <?php check_field('adresa'); ?>align="right">
					 			<label for="adresa"><?php echo LANG_ADRESA; ?></label>
					 		</td>							 		
					 		<td <?php check_field('id_firma_sol'); ?>align="left" width="100">
								<textarea <?php echo show_tooltip(LANG_firma_adresa_tooltip); ?> id="adresa" name="adresa" cols="30" rows="3"><?php echo $_SESSION['firma']->adresa->adresa; ?></textarea>					
							</td>
					 		<td>
								<table align="left">
									<tr>
								 		<td <?php check_field('oras'); ?>align="right">
								 			<label for="oras"><?php echo LANG_ORAS /*. CT_OBLIGATORIU; */ ?></label>
								 		</td>
								 		<td <?php check_field('id_firma_sol'); ?>align="left">
											<input size="25" type="text" id="oras" name="oras" maxlength="<?php echo CT_MAX_ORAS_LENGTH; ?>" value="<?php echo $_SESSION['firma']->adresa->oras; ?>"/>					
										</td>
																	
										<td <?php check_field('cod_postal'); ?>align="right">
								 			<label for="cod_postal"><?php echo LANG_COD_POSTAL; ?></label>
								 		</td>
								 		<td>
											<input <?php echo show_tooltip(LANG_cod_postal_tooltip); ?> size="25" type="text" id="cod_postal" name="cod_postal" maxlength="<?php echo CT_MAX_COD_POSTAL_LENGTH; ?>" value="<?php echo $_SESSION['firma']->adresa->cod_postal; ?>"/>					
										</td>
									</tr>
									<tr>
								 		<td <?php check_field('judet'); ?>align="right">
								 			<label for="judet"><?php echo LANG_JUDET; ?></label>
								 		</td>
								 		<td>
											<input size="25" type="text" id="judet" name="judet" maxlength="<?php echo CT_MAX_JUDET_LENGTH; ?>" value="<?php echo $_SESSION['firma']->adresa->judet; ?>"/>					
										</td>
								 		<td <?php check_field('tara'); ?>align="right">
								 			<label for="tara"><?php echo LANG_TARA /*. CT_OBLIGATORIU; */ ?></label>
								 		</td>
								 		<td colspan="3">
											<input size="25" type="text" id="tara" name="tara" maxlength="<?php echo CT_MAX_TARA_LENGTH; ?>" value="<?php echo $_SESSION['firma']->adresa->tara; ?>"/>					
										</td>									
									</tr>
								</table>
							</td>
				 		</tr>				 		
			 		</table>
		 		</div>
		 		
				<div class="container">
					<table width="600" cellpadding=0 cellspacing="0" border="0">
					  	<tr>
                             <td id="td_conturi" width="320">
                                 <?php                                      
                                     render_cont_table($_SESSION['firma'], false, $currencies, CT_CONT_FIRMA); 
                                 ?>
                              </td>                 
                        </tr>
                        <tr>
					 		<td align="left" valign="top">
						 		<div id="mainContainer">
						 			<input type="button" onclick="javascript: arata_modal_cont(<?php echo CT_CONT_FIRMA; ?>)" value="<?php echo LANG_CREATE_NEW_CONT; ?>" />
						 		</div>	
						 		<br />					 		
					 	 	</td>
					  	</tr>									
					  	<tr>
                          <td id="td_contacte">                                           
                             <?php                          
                                 render_contact_table($_SESSION['firma']); 
                             ?>                                 
                          </td>                              
                        </tr>	                        
					  	<tr>
					 	 	<td align="left" valign="top">					 	 		
					 			<div id="mainContainer">
					 				<input type="button" onclick="javascript: arata_modal_contact();" value="<?php echo LANG_CREATE_NEW_CONTACT; ?>" />
					 			</div>	
					 			<br />				 			
					 	 	</td>					  	
					  	</tr>		
					  	<tr>
                          <td id="td_rute_firme">                                           
                             <?php                          
                                 render_rute_table($_SESSION['firma']); 
                             ?>                                 
                          </td>                              
                        </tr>	
					  	<tr>
					 	 	<td align="left" valign="top">					 	 		
					 			<div id="mainContainer">
					 				<input type="button" onclick="javascript: arata_modal_rute();" value="<?php echo LANG_Create_new_route; ?>" />
					 			</div>	
					 			<br />				 			
					 	 	</td>					  	
					  	</tr>		                        					  	 	
			 		</table>		
			 	</div>
			 	
			 	<div class="container">
			   		<table width="600" border="0" align="left" cellpadding="0" cellspacing="0">
				 	  	<tr>
							<td colspan="2" align="left">
								<?php 
									$_SESSION['firma']->id_firma == CT_NOT_SAVED_YET ? $value_submit = LANG_ADD_FIRMA : $value_submit = LANG_MODIFY;							
								?>
								
								<?php
									buton_final_ok($value_submit, "post_firma");									
									echo '&nbsp;&nbsp;';
									buton_final_cancel(LANG_Cancel, "show_firms.php");
								?>
						 	</td>
					  	</tr>				
			   		</table>
		   		</div>
		   	</form>		
	   	</div> 
	<?php
	}

	unset($_SESSION['wrong_ids']);
	html_footer();
?>